package cn.kmust.booth.portal.controller;


import cn.kmust.booth.portal.model.Admin;
import cn.kmust.booth.portal.security.UserInfo;
import cn.kmust.booth.portal.service.IAdminService;
import cn.kmust.booth.portal.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kmust.cn
 * @since 2020-08-06
 */
@Controller
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @GetMapping("/login.html")
    public String login(){
        return "login";
    }

    @GetMapping("/index.html")
    @ResponseBody
    public String index(@AuthenticationPrincipal UserInfo userInfo){

        return userInfo.getUsername() + userInfo.getCity();

    }

    //添加管理员
    //http://localhost:8080/admin/add?jobNumber=3&password=123456&phone=18787128101
    @GetMapping("/admin/add")
    @ResponseBody
    public R<Void> addAdmin(Admin admin){
        //把异常抛出 进行统一处理异常的方法 GlobalExceptionHandler类中的方法
        adminService.addAdmin(admin);
        return R.ok();
    }

    //忘记密码
    @GetMapping("/update/password")
    @ResponseBody
    public R<Void> updatePwd(String phone,String newPassword,String sms){
        adminService.updatePassword(phone,newPassword,sms);
        return R.ok();
    }
}
